$(function(){
	
	
	$('div.move ul').each(function(){
		$('<li onclick="menu_index_click(this);"><span class="d-bg'+($(this).index()==0?' c-active':'')+'"></span><span class="d-txt">'+($(this).index()+1)+'</span></li>').appendTo($('div.direction-bottom ul'));
	});
	
	var send_loading = $("#send_loading");
	var send_loading_width = 100;
	var send_loading_begin = true;
	var send_auto_add_count = 2;
	var wival_send = null;
	$.ajaxSetup({ 
    	beforeSend:function(){
    		send_loading_width = 100;
    		send_loading_begin = true;
    		send_auto_add_count = 2;
    		send_loading.css({width:100}).show();
    		clearInterval(wival_send);
    		wival_send = setInterval(function(){
    			if(!send_loading_begin){
    				send_auto_add_count=10;
    			}
    			if(send_loading_width>body_width-(body_width*0.3)){
    				if(send_loading_begin){
	    				send_auto_add_count=0;
    				}else{
    					if(send_loading_width>=body_width-11){
    						send_loading.hide();
    						clearInterval(wival_send);
    					}
    				}
    			}
    			if(send_loading_width>=body_width-11){
    				send_loading.hide();
					clearInterval(wival_send);
				}
    			send_loading.css({
    				'width':send_loading_width+=send_auto_add_count
    			});
    		});
    	},
        statusCode:{  
            //未登录或登陆后session失效时，服务端会返回状态码403  
            404:function(data){
            	send_loading_begin = false;
            	alert('404程序找不到页面,请联系管理员.');
            },
            403:function(data){
            	send_loading_begin = false;
            	console.info(data.responseText.toJSON());
            },
            200:function(){
            	send_loading_begin = false;
            }
        }  
    });
	
	init_resize();
});

function init_resize(){
	$("#init-data").height($(window).height()-70);
	body_width = $(document.body).width();
	body_height = $(document.body).height();
}

window.onresize = init_resize;

function menu_item_open(){
	var $self = $(arguments[0]);
	
	var request_paramters = $self.find('input[name=request_paramters]').val();//获取提交参数
	var request_url = $self.find('input[name=request_url]').val();//请求地址
	var request_method = $self.find('input[name=request_method]').val();//请求方式
	
	
	$('#data-main').css({
		'z-index':3
	});
	$('#data-main #d-bg').show().animate({
		'top':0
	},{
		duration: 600,
		easing:'easeInOutQuad',
		complete:function(){
			$('#data-main #close').fadeIn();
			$('#data-main #init-data').show();
			
			$.ajax({
				type:request_method,
				url:request_url,
				async:true,
				data:request_paramters.toJSON(),
				success:function(){
					$("#init-data").hide().html(arguments[0]).fadeIn();
				}
			});
		}
	},1000);
}

function menu_list_item_enter_or_leave(){
	var $self = $(arguments[0]);
	$self.find('span.c-txt').toggleClass('enter');
}

function menu_index_click(){
	var $self = $(arguments[0]);
	
	sign_left_right = -($self.index())*600;
	
	$('.move').animate({
		'margin-left':sign_left_right
	},{
		duration: 600,
		easing:'easeOutBounce'
	});
	$self.parent().find('.c-active').toggleClass('c-active');
	$($self).find('.d-bg').toggleClass('c-active');
}


var sign_left_right = true;
function direction_left_click(){
	var $self = $(arguments[0]);
	var $move_box = $('div.move-box');
	var move_box_width = $move_box.width();
	var indexs = $('div.direction-bottom span.d-bg');
	if(indexs.filter('.c-active').parent().index()>0){
		indexs.filter('.c-active').toggleClass('c-active').parent().prev().find('.d-bg').toggleClass('c-active');
		sign_left_right+=move_box_width;
	}else{
    	if(!$move_box.is(':animated')){
			for(var i=1; 4>=i; i++){
		    	$move_box.animate({left:(40-10*i)},50).animate({left:2*(40-10*i)-1},50);
		    }
    	}
	}
	
	$('.move').animate({
		'margin-left':sign_left_right
	},{
		duration: 600,
		easing:'easeInOutCirc'
	});
	
}


function direction_right_click(){
	var $self = $(arguments[0]);
	var $move_box = $('div.move-box');
	var move_box_width = $move_box.width();
	var indexs = $('div.direction-bottom span.d-bg');
	
	if(indexs.filter('.c-active').parent().index()!=indexs.length-1){
		indexs.filter('.c-active').toggleClass('c-active').parent().next().find('.d-bg').toggleClass('c-active');
		sign_left_right-=move_box_width;
	}else{
		if(!$move_box.is(':animated')){
			for(var i=1; 4>=i; i++){
		    	$move_box.animate({left:(40-10*i)},50).animate({left:2*(40-10*i)-1},50);
		    }
		}
	}
	
	$('.move').animate({
		'margin-left':sign_left_right
	},{
		duration: 600,
		easing:'easeInOutCirc'
	});
	
}

function close_enter(){
	var $self = $(arguments[0]);
	$self.find('ul').each(function(){
		$(this).dequeue();
	}).animate({
		right:0
	});
}

function close_leave(){
	var $self = $(arguments[0]);
	$self.find('ul').each(function(){
		$(this).dequeue();
	}).animate({
		right:-20
	});
}

function filter_close_click(){
	var $self = $(arguments[0]);
	$("#send_loading").hide();//进度条
	$('#data-main #init-data').fadeOut(function(){
		$(this).empty();
	});
	$('#data-main #close-left,#data-main #close-right').css({
		width:'50%'
	}).show();
	$('#d-bg').hide().css({
		top:-$(window).height()
	});
	$('#close-left').animate({
		width:'0%'
	});
	$('#data-main #close-right').animate({
		width:'0%'
	},function(){
		$('#data-main #close').fadeOut();
	});
	$('#data-main').css('z-index',-1);
}

function bar_left_enter(){
	var $self = $(arguments[0]);
	$self.each(function(){
		$(this).dequeue();
	}).animate({
		left:0
	});
}

function bar_left_leave(){
	var $self = $(arguments[0]);
	$self.each(function(){
		$(this).dequeue();
	}).animate({
		left:-64
	});
}

function correct_name_value(){//纠正用户名
	var $self = $(arguments[0]);
	var $value = $self.parent().parent().find('.name-value');
	$('<input type="text" onfocusout="correct_name_value_focusout(this);" value="'+$value.text()+'" />').appendTo($value.empty());
}

function correct_name_value_focusout(){
	var $self = $(arguments[0]);
	var _val = $self.val();
	$self.parents('form').find('input[name=username]').val(_val);
	$self.parent().empty().text(_val);
}
